package com.sys.servlet;



import com.sys.entity.Concert;
import com.sys.service.ConcertService;
import com.sys.service.impl.ConcertServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/cAdd")
public class SysConcertServletAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out =resp.getWriter();
        Concert concert=new Concert();
        concert.setConcertName(req.getParameter("concertName"));
        concert.setCity(req.getParameter("city"));
        concert.setPrice(Double.parseDouble(req.getParameter("price")));
        concert.setRecommend(Integer.parseInt(req.getParameter("recommend")));
        try{
            Date date=new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("startTime"));
            concert.setStartTime(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        concert.setAddress(req.getParameter("address"));
        ConcertService server=new ConcertServiceImpl();
        int result=server.addConcert(concert);
        if (result>0){
            out.print("<script>alert('添加成功！');location.href='cPage'</script>");
        }else{
            out.print("<script>alert('添加失败！');location.href='CAdd.jsp'</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
